package com.sharedmobility.servlet;

import com.google.gson.Gson;
import com.sharedmobility.dao.TripDAO;
import com.sharedmobility.model.Trip;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

@WebServlet("/api/rating")
public class RatingServlet extends HttpServlet {
    private TripDAO tripDAO;
    private Gson gson;

    @Override
    public void init() throws ServletException {
        tripDAO = new TripDAO();
        gson = new Gson();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        try {
            // 获取当前登录用户ID
            HttpSession session = request.getSession();
            Integer userId = (Integer) session.getAttribute("userId");
            
            if (userId == null) {
                throw new Exception("用户未登录");
            }

            // 获取请求参数
            Map<String, Object> requestData = gson.fromJson(request.getReader(), Map.class);
            Integer tripId = ((Number) requestData.get("tripId")).intValue();
            Integer rating = ((Number) requestData.get("rating")).intValue();
            String comment = (String) requestData.get("comment");

            // 验证评分范围
            if (rating < 1 || rating > 5) {
                throw new Exception("评分必须在1-5之间");
            }

            // 获取行程信息
            Trip trip = tripDAO.findById(tripId);
            if (trip == null) {
                throw new Exception("行程不存在");
            }
            if (!trip.getUserId().equals(userId)) {
                throw new Exception("无权评价此行程");
            }
            if (trip.getStatus() != 2) {
                throw new Exception("只能评价已完成的行程");
            }
            if (trip.getRating() != null) {
                throw new Exception("该行程已评价");
            }

            // 更新评分
            trip.setRating(rating);
            trip.setComment(comment);
            boolean success = tripDAO.update(trip);
            if (!success) {
                throw new Exception("更新评分失败");
            }

            // 返回成功响应
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "评价成功");
            
            response.getWriter().write(gson.toJson(result));
        } catch (Exception e) {
            e.printStackTrace(); // 打印详细异常
            // 返回错误响应
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", e.getMessage());
            
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write(gson.toJson(result));
        }
    }
} 